package cn.lzh.service;


import cn.lzh.entity.Cart;
import cn.lzh.entity.CartBook;
import cn.lzh.mapper.BookMapper;
import cn.lzh.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService{
   @Autowired
   CartMapper cartMapper;
   @Autowired
   BookMapper bookMapper;
    @Override
    public Cart findOne(int id) {
        return cartMapper.findOne(id);
    }

    @Override
    public List<CartBook> findByUid(int uid) {
        return cartMapper.findByUid(uid);
    }

    @Override
    public void remove(int id) {
    cartMapper.datee(id);
    }

    @Override
    public void update(int id, int num) {
      cartMapper.update(id,num);
    }

    @Override
    @Transactional //开启事务
    public void add(Cart cart) {
        Map<String,Object> map = new HashMap<>();
        map.put("bookid",cart.getBookid());
        map.put("num",1);
        bookMapper.addFavnum(map);
        Cart c = cartMapper.findByBookidUid(cart.getUid(), cart.getBookid());
        cart.setTcreate(new Date());
        if (c == null) {
            this.cartMapper.add(cart);
        }
    }


}
